﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using OurSite.Business;
using OurSite.Business.Event;
using OurSite.Business.Picture;


namespace OurSite.Web.HTMLHelpers
{
    public static class HTMLHelper
    {
        static string prefix = ConfigurationManager.AppSettings["ImagePrefix"];

        public static string BuildYearMenuItem(string title, string parentAction, string childAction, IList<int> years, int showNumber, string itemName, string selectedItem)
        {
            StringBuilder sb = new StringBuilder();
            IList<BaseClass> list = new List<BaseClass>();
            foreach (int year in years.Reverse())
            {
                BaseClass baseClass = new BaseClass();
                baseClass.Name = year.ToString();
                list.Add(baseClass);
            }

            return BuildMenuItem(title, parentAction, childAction, list, showNumber, itemName, selectedItem);
        }

        public static string BuildRegionMenuItem(string title, string parentAction, string childAction, IList<string> regions, int showNumber, string itemName, string selectedItem)
        {
            StringBuilder sb = new StringBuilder();
            IList<BaseClass> list = new List<BaseClass>();
            foreach (string region in regions)
            {
                BaseClass baseClass = new BaseClass();
                baseClass.Name = region;
                list.Add(baseClass);
            }

            return BuildMenuItem(title, parentAction, childAction, list, showNumber, itemName, selectedItem);
        }

        public static string BuildMenuItem<T>(string title, string parentAction, string childAction, IList<T> items, int showNumber, string itemName, string selectedItem) where T : BaseClass
        {
            StringBuilder sb = new StringBuilder();
            StringBuilder titleSection = new StringBuilder();
            StringBuilder firstSection = new StringBuilder();
            StringBuilder secondSection = new StringBuilder();
            StringBuilder lessSection = new StringBuilder();

            string moreLink = "more" + itemName + "link";
            string lessLink = "less" + itemName + "link";
            string moreDiv = "by" + itemName + "More";

            titleSection.Append("<h1><a href='/photos.aspx/" + parentAction + "'>" + title + "</a></h1>");

            int counter = 0;
            bool hasSelectedItem = false;

            if (items.Count() == 0)
                titleSection.Append("<p>Coming soon...</p>");

            foreach (var item in items)
            {
                string linkFormat = string.Empty;
                if (item.Name == selectedItem)
                {
                    linkFormat = "<p><a href=\"/photos.aspx/{0}/{1}\" class='highlight'>{2}</a></p>";
                    hasSelectedItem = true;
                }
                else
                    linkFormat = "<p><a href=\"/photos.aspx/{0}/{1}\">{2}</a></p>";

                if(counter < showNumber)
                    firstSection.Append(string.Format(linkFormat, childAction, item.URLFriendly, item.Name));

                if (counter >= showNumber)
                    secondSection.Append(string.Format(linkFormat, childAction, item.URLFriendly, item.Name));

                counter++;
            }

            if (items.Count > showNumber)
                lessSection.Append(string.Format("<p><a id=\"{2}\" class=\"cursor\" onclick=\"ShowMore('{1}', '{0}', '{2}')\">...less</a></p>", moreLink, moreDiv, lessLink));


            sb.Append(titleSection);
            sb.Append(firstSection);
            if (items.Count > showNumber)
            {
                if(hasSelectedItem)
                    sb.Append(string.Format("<p><a id=\"{0}\" class=\"cursor displayNone\" onclick=\"ShowMore('{1}', '{0}', '{2}')\">more...</a></p> <div id=\"{1}\" >", moreLink, moreDiv, lessLink));
                else
                    sb.Append(string.Format("<p><a id=\"{0}\" class=\"cursor\" onclick=\"ShowMore('{1}', '{0}', '{2}')\">more...</a> <div id=\"{1}\" class=\"displayNone\"></p>", moreLink, moreDiv, lessLink));
            }
            else
                sb.Append(string.Format("<div id=\"{0}\" >", moreDiv));
            sb.Append(secondSection);
            sb.Append(lessSection + "</div>");

            return sb.ToString();
        }        

        public static string PictureSearchResults(List<Picture> pictures)
        {
            StringBuilder pics = new StringBuilder();

            pics.Append("<section id=\"searchResults\">");
            pics.Append("<header>");
            pics.Append("<h1>Search Results</h1> ");
            pics.Append("</header>");
            pics.Append("<article>");
            pictures.ForEach(p =>
                                 {
                                     pics.Append("<div class=\"gallery-item\">");
                                     pics.Append("<a class=\"galleryThumb galleryThumbImage\" href=\"\" title=\""+ p.Caption + "\">");
                                     pics.Append("<i style=\"background-image: url('" + prefix + "small" + p.ImagePath + "')\"></i>");
                                     pics.Append("</a>");
                                     pics.Append("</div>");
                                 });
            pics.Append("</article>");
            pics.Append("</section>");
            pics.Append("<div class=\"clear\"></div>");
            return pics.ToString();
        }
    }

}
